﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using QLTV_BLL;
using QLTV_Public;

namespace GUI
{
    public partial class uc_DocGia : DevExpress.XtraEditors.XtraUserControl
    {
        #region khai bao bien

        TextMessageBox box = new TextMessageBox();
        DocGia_BLL dg_bll = new DocGia_BLL();
        DataTable dt;
        private bool isGV = true;
        string maDocGia = "";

        //DateTime fromNgayHetHan;
        DateTime toNgayHetHan;
        //DateTime fromNgayLamThe;
        DateTime toNgayLamThe;
        DateTime ngaySinh;
        bool isNgaySinh = false;
        bool isNgayHetHan = false;
        bool isNgayLamThe = true;
        #endregion

        #region Loadform

        public uc_DocGia()
        {
            InitializeComponent();
        }

        private void uc_DocGia_Load(object sender, EventArgs e)
        {
            
            LoadDataGiaoVien();
        }
        #endregion

        #region cac chuc nang

        private void btnThemMoi_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (isGV == true)//hiện form giao viên
            {
                frmGiaoVien frm = new frmGiaoVien();
                if (frm.ShowDialog() == DialogResult.OK)
                    LoadDataGiaoVien();
            }
            else//hiện form Học viên
            {
                frmSinhVien frm = new frmSinhVien();
                if (frm.ShowDialog() == DialogResult.OK)
                    LoadDataHocVien();
            }
        }

        private void btnSua_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (isGV == true)//hiện form giao viên
            {
                frmGiaoVien frm = new frmGiaoVien();
                frm.maDocGia = grvData_GiaoVien.GetRowCellValue(grvData_GiaoVien.FocusedRowHandle, colMaDocGia).ToString();
                if (frm.ShowDialog() == DialogResult.OK)
                    LoadDataGiaoVien();
            }
            else//hiện form Học viên
            {
                frmSinhVien frm = new frmSinhVien();
                frm.maDocGia = grvData_HocVien.GetRowCellValue(grvData_HocVien.FocusedRowHandle, colMaDocGiaHV).ToString();
                if (frm.ShowDialog() == DialogResult.OK)
                    LoadDataHocVien();
            }
        }

        private void btnXoa_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            
            if (isGV == true)//hiện form giao viên
            {
                string ma = grvData_GiaoVien.GetRowCellValue(grvData_GiaoVien.FocusedRowHandle, colMaDocGia).ToString();
                string ten = grvData_GiaoVien.GetRowCellValue(grvData_GiaoVien.FocusedRowHandle, colHoVaTen).ToString();
                dt = dg_bll.TaoBang(box.sqlCheckExist("PhieuYeuCau", "MaDocGia", ma));
                if (dt.Rows.Count > 0)
                {
                    XtraMessageBox.Show(box.NotDelete(ma), box.Caption, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    if (XtraMessageBox.Show(box.Delete_Confirm(ma, ten), box.Caption_Confirm, MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                    {
                        try
                        {
                            dg_bll.Delete_DocGia_GiaoVien(ma);
                            LoadDataGiaoVien();
                        }
                        catch (Exception)
                        {
                            XtraMessageBox.Show(box.DeleteFail,box.Caption_Fail,MessageBoxButtons.OK,MessageBoxIcon.Error);
                        }
                    }
                }
            }
            else//hiện form Học viên
            {
                string ma = grvData_HocVien.GetRowCellValue(grvData_HocVien.FocusedRowHandle, colMaDocGiaHV).ToString();
                string ten = grvData_HocVien.GetRowCellValue(grvData_HocVien.FocusedRowHandle, colHoVaTenHV).ToString();
                dt = dg_bll.TaoBang(box.sqlCheckExist("PhieuYeuCau", "MaDocGia", ma));
                if (dt.Rows.Count > 0)
                {
                    XtraMessageBox.Show(box.NotDelete(ma), box.Caption, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    if (XtraMessageBox.Show(box.Delete_Confirm(ma, ten), box.Caption_Confirm, MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                    {
                        try
                        {
                            dg_bll.Delete_DocGia_HocVien(ma);
                            LoadDataHocVien();
                        }
                        catch (Exception)
                        {
                            XtraMessageBox.Show(box.DeleteFail, box.Caption_Fail, MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    }
                }
            }
        }
        private void btnGiaoVien_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            isGV = true;
            pnlHocVien.Hide();
            pnlGiaoVien.Show();
            pnlGiaoVien.Dock = DockStyle.Fill;
            LoadDataGiaoVien();
        }

        private void btnHocVien_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            isGV = false;
            pnlGiaoVien.Hide();
            pnlHocVien.Show();
            pnlHocVien.Dock = DockStyle.Fill;
            LoadDataHocVien();
        }
        #endregion

        #region ham viet  them
        private void Search_Data()
        {
            if (isGV == true)
            {
                grdData_GiaoVien.DataSource = dg_bll.Load_DocGia_GiaoVien(txtMaDocGia.Text.Trim(), txtHOTen.Text, isNgaySinh, dtpNgaySinh.Value, isNgayLamThe, dtpNgayLamTheFrom.Value, dtpNgayLamTheTo.Value, isNgayHetHan, dtpNgayHetHanFrom.Value, dtpNgayHetHanTo.Value);
                grvData_GiaoVien.BestFitColumns();
                this.colAnh.Width = 100;
            }
            else
            {
                grdData_HocVien.DataSource = dg_bll.Load_DocGia_HocVien(txtMaDocGia.Text.Trim(), txtHOTen.Text, isNgaySinh, dtpNgaySinh.Value, isNgayLamThe, dtpNgayLamTheFrom.Value, dtpNgayLamTheTo.Value, isNgayHetHan, dtpNgayHetHanFrom.Value, dtpNgayHetHanTo.Value);
                grvData_HocVien.BestFitColumns();
                this.colAnhHV.Width = 100;
            }
        }
        private void LoadDataGiaoVien()
        {
            pnlHocVien.Hide();
            pnlGiaoVien.Show();
            pnlGiaoVien.Dock = DockStyle.Fill;
            
           grdData_GiaoVien.DataSource = dg_bll.Load_DocGia_GiaoVien(txtMaDocGia.Text.Trim(),txtHOTen.Text,isNgaySinh,dtpNgaySinh.Value,isNgayLamThe,dtpNgayLamTheFrom.Value,dtpNgayLamTheTo.Value,isNgayHetHan,dtpNgayHetHanFrom.Value,dtpNgayHetHanTo.Value);
            grvData_GiaoVien.BestFitColumns();
            this.colAnh.Width = 100;
        }
        private void LoadDataHocVien()
        {
            pnlGiaoVien.Hide();
            pnlHocVien.Show();
            pnlHocVien.Dock = DockStyle.Fill;
            grdData_HocVien.DataSource = dg_bll.Load_DocGia_HocVien(txtMaDocGia.Text.Trim(), txtHOTen.Text, isNgaySinh, dtpNgaySinh.Value, isNgayLamThe, dtpNgayLamTheFrom.Value, dtpNgayLamTheTo.Value, isNgayHetHan, dtpNgayHetHanFrom.Value, dtpNgayHetHanTo.Value);
            grvData_HocVien.BestFitColumns();
            this.colAnhHV.Width = 100;
        }

        private void Lay_Data_GiaHan()
        {
            int d = 0;
             if (isGV == true)
            {
                grvData_GiaoVien.CloseEditor();
                grvData_GiaoVien.UpdateCurrentRow();
                for (int i = 0; i < grvData_GiaoVien.RowCount; i++)
                {
                    if (Convert.ToBoolean(grvData_GiaoVien.GetRowCellValue(i, colCheckGV)) == true)
                    {
                        maDocGia += grvData_GiaoVien.GetRowCellValue(i, colMaDocGia).ToString() + ",";
                        d = d + 1;
                    }
                }
                if (d > 0)
                {
                    frmGiaHanThe frm = new frmGiaHanThe();
                    frm.maDocGia = maDocGia;
                    if (frm.ShowDialog() == DialogResult.OK)
                        LoadDataGiaoVien();
                }
                else if (d == 0)
                    XtraMessageBox.Show("Bạn chưa chọn Độc giả để gia hạn thẻ.",box.Caption,MessageBoxButtons.OK,MessageBoxIcon.Warning);
            }
            else
            {
                grvData_HocVien.CloseEditor();
                grvData_HocVien.UpdateCurrentRow();
                for (int i = 0; i < grvData_HocVien.RowCount; i++)
                {
                    if (Convert.ToBoolean(grvData_HocVien.GetRowCellValue(i, colCheckHV)) == true)
                    {
                        maDocGia += grvData_HocVien.GetRowCellValue(i, colMaDocGiaHV).ToString() + ",";
                        d = d + 1;
                    }
                }
                if (d > 0)
                {
                    frmGiaHanThe frm = new frmGiaHanThe();
                    frm.maDocGia = maDocGia;
                    if (frm.ShowDialog() == DialogResult.OK)
                        LoadDataHocVien();
                }
                else if (d == 0)
                    XtraMessageBox.Show("Bạn chưa chọn Độc giả để gia hạn thẻ.", box.Caption, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
        #endregion

        private void btnCheckAll_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (isGV == true)
            {
                for (int i = 0; i < grvData_GiaoVien.RowCount; i++)
                    grvData_GiaoVien.SetRowCellValue(i, colCheckGV, true);
            }
            else
            {
                for (int i = 0; i < grvData_HocVien.RowCount; i++)
                    grvData_HocVien.SetRowCellValue(i, colCheckHV, true);
            }
        }

        private void btnunCheck_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (isGV == true)
            {
                for (int i = 0; i < grvData_GiaoVien.RowCount; i++)
                    grvData_GiaoVien.SetRowCellValue(i, colCheckGV, false);
            }
            else
            {
                for (int i = 0; i < grvData_HocVien.RowCount; i++)
                    grvData_HocVien.SetRowCellValue(i, colCheckHV, false);
            }
        }

        private void btnGiaHanThe_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Lay_Data_GiaHan();
           
        }

        private void btnTimKiem_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Search_Data();
        }

        private void ckbNgaySinh_CheckedChanged(object sender, EventArgs e)
        {
            if (ckbNgaySinh.CheckState == CheckState.Checked)
            {
                isNgaySinh = true;
                dtpNgaySinh.Enabled = true;
            }
            else
            {
                isNgaySinh = false;
                dtpNgaySinh.Enabled = false;
            }

        }


        private void ckbNgayLamThe_CheckedChanged(object sender, EventArgs e)
        {
            if (ckbNgayLamThe.CheckState == CheckState.Checked)
            {
                isNgayLamThe = true;
                dtpNgayLamTheFrom.Enabled = true;
                dtpNgayLamTheTo.Enabled = true;
            }
            else
            {
                isNgayLamThe = false;
                dtpNgayLamTheFrom.Enabled = false;
                dtpNgayLamTheTo.Enabled = false;
            }
        }

        private void ckbNgayHetHan_CheckedChanged(object sender, EventArgs e)
        {
            if (ckbNgayHetHan.CheckState == CheckState.Checked)
            {
                isNgayHetHan = true;
                dtpNgayHetHanFrom.Enabled = true;
                dtpNgayHetHanTo.Enabled = true;
            }
            else
            {
                isNgayHetHan = false;
                dtpNgayHetHanFrom.Enabled = false;
                dtpNgayHetHanTo.Enabled = false;
            }
        }

        private void grvData_GiaoVien_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e)
        {
            try
            {
                //
                if ((e.RowHandle != -1) && (grvData_GiaoVien.GetRowCellValue(e.RowHandle, colTrangThaiSD).ToString() == "Không Hoạt Động"))
                {

                    e.Appearance.BackColor = System.Drawing.Color.Red;

                }
            }
            catch { }
        }

        private void grvData_HocVien_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e)
        {
            try
            {
               if ((e.RowHandle != -1) && grvData_HocVien.GetRowCellValue(e.RowHandle, colTrangThaiSDHV).ToString() == "Không Hoạt Động")
                {

                    e.Appearance.BackColor = System.Drawing.Color.Red;

                }
            }
            catch { }
        }
        



    }
}
